package com.apusic.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description 时间常用方法类
 */
public class DateUtil {

	/**
	 * @author:陈庆钊
	 * 功能：得到当前时间
	 * @return dateNowStr
	 */
	public static String getNow(){
		
		Date dateNow=new Date();

		SimpleDateFormat  dateFormat=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");

		String dateNowStr=dateFormat.format(dateNow);
		
		return dateNowStr;
	}
	
	public static String getTime(){
		Long l = System.currentTimeMillis();
		return l.toString();
	}
	
	public static List<String> getLastYears(int num){
		
		List<String> yearList = new ArrayList<String>();
		for(int i=num;i<=0;i++){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");  
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.YEAR,i);
			String date = sdf.format(cal.getTime());
			yearList.add(date);
		}
		return yearList;
	}
	
	public static int getCurrentYear(){
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	
	//返回指定日期往前的十年集合
	public static List<String> getLastTenYears(String specifiedDate){
		List<String> yearList = new ArrayList<String>();
		Integer endYear = Integer.parseInt(specifiedDate.substring(0,4));
		for(int i=9;i>=0;i--){
			String year = (endYear - i)+"";
			yearList.add(year);
		}
		return yearList;
	}
	
	//返回最近12个季度的统计日期集合
	public static List<String> getStatLastTenYears(String specifiedDate){
		List<String> yearList = new ArrayList<String>();
		Integer endYear = Integer.parseInt(specifiedDate.substring(0,4));
		for(int i=9;i>=0;i--){
			String year = (endYear - i)+"-12-31";
			yearList.add(year);
		}
		return yearList;
	}
	
	public static List<String> getStatLastTwelveSeasons(String specifiedDate){
		List<String> seasonList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for(int i=11;i>=0;i--){
			try {
				Calendar statCalendar = Calendar.getInstance();
				Date sdate = sdf.parse(specifiedDate);
				statCalendar.setTime(sdate);
				statCalendar.add(Calendar.MONDAY, -i*3);
				String ndate = sdf.format(statCalendar.getTime());
				String year = ndate.substring(0, 4);
				String  season = getSeason(sdf.parse(ndate))+"";
				String statDate = getStatDate(year,season);
				seasonList.add(statDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return seasonList;
	}
	
	//获取指定时间的年份
	public static String getSpecifiedYear(String specifiedDate){
		return specifiedDate.substring(0,4);
	}
	
	//获取指定时间的季度
	public static String getSpecifiedSeason(String specifiedDate){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(specifiedDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		String season = getSeason(date)+"";
		return season;
	}
	
	//指定日期，减去指定的月份，返回时间
	public static String dateSubMonth(String specifiedDate,int months){
		String statDate = "";
		Calendar statCalendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date sdate = sdf.parse(specifiedDate);
			statCalendar.setTime(sdate);
			statCalendar.add(Calendar.MONTH, -months);
			statDate = sdf.format(statCalendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return statDate;
	}
	
	public static String dateSubDay(String specifiedDate,int days){
		String statDate = "";
		Calendar statCalendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date sdate = sdf.parse(specifiedDate);
			statCalendar.setTime(sdate);
			statCalendar.add(Calendar.DAY_OF_MONTH, -days);
			statDate = sdf.format(statCalendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return statDate;
	}
	
	public static String getStatDate(String year,String season){
		String statDate = "";
		if(season.equals("1")){
			statDate = year + "-03-31";
		}else if(season.equals("2")){
			statDate = year + "-06-30";
		}else if(season.equals("3")){
			statDate = year + "-09-30";
		}else{
			statDate = year + "-12-31";
		}
		return statDate;
	}
	
	/**
	 * 返回过去三十天的日期集合
	 * @return
	 */
	public static List<String> getLastThirtyDays(){
		List<String> days = new ArrayList<String>();
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = df.format(date);
		for(int i=29;i>=0;i--){
			String statDay = dateSubDay(currentDate,i);
			days.add(statDay);
		}
		return days;
	}
	
	
	
	
	public static void main(String[] args) {
//		List<String> dates = DateUtil.getLastYears(0);
//		for(String s:dates){
//			System.out.println(s);
//		}
		
//		List<String> lastTenYears = getLastTenYears("2016-12-28");
//		for(String  year: lastTenYears){
//			System.out.println(year);
//		}
		
//		List<String> twelveSeasons = getStatLastTwelveSeasons("2017-10-11");
//		for(String season:twelveSeasons){
//			System.out.println(season);
//		}
//		
		System.out.println(getLastThirtyDays());
	}
	public static String getNowWeek(){
		Calendar c=Calendar.getInstance();
        int i = c.get(Calendar.WEEK_OF_YEAR);
        return i+1+"";
	}
	public static String getLastDate(int day){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,day);
		String tomorrow = sdf.format(cal.getTime());
		return tomorrow;
	}
	public static String getLastMonth(int month,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);  
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH,month);
		String lastMonth = sdf.format(cal.getTime());
		return lastMonth;
	}
	/*
	 * 获取俩个日期之间的月份
	 */
	public static ArrayList<String> getBetweenMonth(String startTime,String endTime){
		ArrayList<String> result = new ArrayList<String>();  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月  
	  
	    Calendar min = Calendar.getInstance();  
	    Calendar max = Calendar.getInstance();  
	  
	    try {
			min.setTime(sdf.parse(startTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}  
	    min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);  
	  
	    try {
			max.setTime(sdf.parse(endTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}  
	    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);  
	  
	    Calendar curr = min;  
	    while (curr.before(max)) {  
	     result.add(sdf.format(curr.getTime()));  
	     curr.add(Calendar.MONTH, 1);  
	    }  
	    return result;
	}
	public static String getDay(){
		Date dateNow=new Date();

		SimpleDateFormat  dateFormat=new SimpleDateFormat ("yyyyMMdd");

		String dateNowStr=dateFormat.format(dateNow);
		
		return dateNowStr;
	}
	
	public static String getDate(Date date){

		SimpleDateFormat  dateFormat=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");

		String dateNowStr=dateFormat.format(date);
		
		return dateNowStr;
	}
	
	public static String getNowMonth(){
		Calendar cale = null;  
        cale = Calendar.getInstance();
        int month = cale.get(Calendar.MONTH) + 1;
        return month+"";
	}
	
	/** 
     *  
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度 
     *  
     * @param date 
     * @return 
     */  
    public static int getSeason(Date date) {  
  
        int season = 0;  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
        switch (month) {  
        case Calendar.JANUARY:  
        case Calendar.FEBRUARY:  
        case Calendar.MARCH:  
            season = 1;  
            break;  
        case Calendar.APRIL:  
        case Calendar.MAY:  
        case Calendar.JUNE:  
            season = 2;  
            break;  
        case Calendar.JULY:  
        case Calendar.AUGUST:  
        case Calendar.SEPTEMBER:  
            season = 3;  
            break;  
        case Calendar.OCTOBER:  
        case Calendar.NOVEMBER:  
        case Calendar.DECEMBER:  
            season = 4;  
            break;  
        default:  
            break;  
        }  
        return season;  
    } 
}
